import { createRouter, createWebHistory } from 'vue-router'
import LoginPage from '@/views/LoginPage.vue'
import RegisterPage from "@/views/RegisterPage.vue";
import AppHome from "@/views/AppHome.vue";
import FileList from "@/components/TheFileList.vue";
import RecycleBin from "@/components/RecycleBin.vue";
import PersonalCenter from "@/components/PersonalCenter.vue";
const routes = [  //创建路由
    {
        path: '/login',
        name: 'Login',
        component: LoginPage,
        meta:{requiresAuth:false}
    },
    {
        path: '/register',
        name: 'Register',
        component: RegisterPage,
    },

    {
        path: '/',
        name: 'Home',
        component: AppHome,
        meta:{requiresAuth:true},
        children: [
            {
                path: '', // 默认子路由
                name: 'fileList',
                component: FileList,
            },
            {
                path: 'all-files',
                name: 'allFiles',
                component: FileList,
            },
            {
                path: 'all-files/:type',
                name: 'filesByType',
                component: FileList,
            },
            {
                path: '/all-files/:path*', // 使用 *: 匹配嵌套路径
                name: 'filesByPath',
                component: FileList,
            },
            {
                path: 'recycle',
                name: 'recycle',
                component: RecycleBin,
            },
            {
                path: 'personal',
                name: 'personal',
                component: PersonalCenter,
            }
        ]
    },
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

// 路由守卫 - 动态权限控制
router.beforeEach((to, from, next) => {
    const isAuthenticated = localStorage.getItem('userInfo')

    if (to.meta.requiresAuth && !isAuthenticated) {
        next('/login')
    } else if (to.path === '/login' && isAuthenticated) {
        next('/')
    } else {
        next()
    }
})
export default router  //导出路由